package com.wxxymaker.controller;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.hook.annotation.AdminIgnore;
import com.wxxymaker.hook.annotation.AdminRequire;
import com.wxxymaker.model.entity.Email;
import com.wxxymaker.service.EmailService;
import io.github.biezhi.anima.page.Page;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Soft
 * @Date: 2018/7/14 13:37
 * @Desc: 邮件控制器
 */
@AdminRequire
@Path(value = "/email", restful = true)
public class EmailController {
    @Inject
    private EmailService service;

    @AdminIgnore
    @PostRoute("/submit")
    public RestResponse submit(Email email) {
        Email add = service.add(email);
        if (add != null) {
            return RestResponse.ok(add);
        }
        return RestResponse.fail();
    }

    @PostRoute("/delete/:id")
    public RestResponse delete(@PathParam int id) {
        if (id > 0) {
            Email one = service.delete(id);
            return RestResponse.ok(one);
        }
        return RestResponse.fail();
    }

    @PostRoute("/change")
    public RestResponse change(Email email) {
        if (service.change(email)) {
            return RestResponse.ok(email);
        }
        return RestResponse.fail();
    }

    @PostRoute("/one/:id")
    public RestResponse find(@PathParam int id) {
        Email one = service.findOne(id);
        if (one != null) {
            return RestResponse.ok(one);
        }
        return RestResponse.fail();
    }

    @PostRoute("/like")
    public RestResponse likeByName(@Param String name) {
        Page<Email> like = service.like(name);
        if (like != null) {
            return RestResponse.ok(like);
        }
        return RestResponse.fail();
    }

    @PostRoute("/page/:no")
    public RestResponse page(@PathParam int no,
                             @Param(defaultValue = "-1") Integer status) {
        Page<Email> page = service.page(no, status);
        if (page != null) {
            return RestResponse.ok(page);
        }
        return RestResponse.fail();
    }

    @PostRoute("/numInfo")
    public RestResponse info() {
        Map<String, Long> info = new HashMap<>();

        long all   = service.count(null); // 全部
        long open  = service.count(1);    // 已读
        long close = service.count(0);    // 未读

        info.put("all", all);
        info.put("open", open);
        info.put("close", close);

        return RestResponse.ok(info);
    }
}
